<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType" id="J_navbar">
    <div class="site-navbar__header">
      <!-- 点击跳转到 home -->
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">东冠快速开发平台</a>
        <a class="site-navbar__brand-mini" href="javascript:;">东冠</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix" id="J_navbar_body">
      <!-- 导航菜单， mode:垂直模式和水平模式 -->
      <el-menu
        class="site-navbar__menu"
        mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
      </el-menu >
      <!-- ===================================================== -->
      <!-- 导航一级菜单栏 -->
      <TopMenu class="site-navbar__menu" />
      <!-- =================================================== -->
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal">
        <el-menu-item index="2">
          <el-badge value="cold">
            <a href="//www.renren.io/" target="_blank">官方社区</a>
          </el-badge>
        </el-menu-item>
        <!-- 子菜单 -->
        <el-submenu index="3">
          <template slot="title">Git源码</template>
          <el-menu-item index="2-1"><a href="//github.com/daxiongYang/renren-fast-vue" target="_blank">前端</a></el-menu-item>
          <el-menu-item index="2-2"><a href="//git.oschina.net/renrenio/renren-fast" target="_blank">后台</a></el-menu-item>
          <el-menu-item index="2-3"><a href="//git.oschina.net/renrenio/renren-generator" target="_blank">代码生成器</a></el-menu-item>
        </el-submenu>
        <el-menu-item class="site-navbar__avatar" index="3">
          <!-- 展开下拉菜单的延时（仅在 trigger 为 hover 时有效） trigger默认 hover -->
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName">{{ userName }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>

        <!-- 设置 -->
         <el-menu-item index="4" @click="themeSettingOpen = !themeSettingOpen">
          <template slot="title">
            <!-- 徽章: 可以设置最大值最小值 -->
            <el-badge title="主题样式">
              <icon-svg name="diamond" class="el-icon-theme"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import { clearLoginInfo } from '@/utils'
  import TopMenu from './main-top-menu'
  export default {
    data () {
      return {
        updatePassowrdVisible: false
      }
    },
    components: {
      UpdatePassword,
      TopMenu
    },
    computed: {
      navbarLayoutType: {
        get () { return this.$store.state.common.navbarLayoutType }
      },
      sidebarFold: {
        get () { return this.$store.state.common.sidebarFold },
        set (val) { this.$store.commit('common/updateSidebarFold', val) }
      },
      mainTabs: {
        get () { return this.$store.state.common.mainTabs },
        set (val) { this.$store.commit('common/updateMainTabs', val) }
      },
      userName: {
        get () { return this.$store.state.user.name }
      },
      themeSettingOpen: {
        get () { return this.$store.state.common.themeSettingOpen },
        set (val) { this.$store.commit('common/updateThemeSettingOpen', val) }
      }
    },
    methods: {
      showThemeSetting () {
        let themeDom = document.getElementById('site-theme')
        themeDom.style.width = '0px'
        console.log('themeDom', themeDom)
      },
      // 修改密码
      updatePasswordHandle () {
        this.updatePassowrdVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 退出
      logoutHandle () {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/logout'),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.code === 0) {
              clearLoginInfo()
              this.$router.push({ name: 'login' })
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>

<style scoped>
.site-navbar__body{
  height: 60px;
}
</style>
